/************************************************************************/
/*  									*/
/*  initialize.c							*/
/*									*/
/*  Tom Lookabaugh							*/
/*  August 13, 1987							*/
/*  Last Revision: August 15, 1987					*/
/*									*/
/*  DESCRIPTION								*/
/*	Computes a size one codebook by finding the centroid of the	*/
/*	entire training sequence.  Reports the distortion when the	*/
/*	training sequence is mapped to its centroid.			*/
/*  RETURNS								*/
/*	Distortion when entire training sequence is encoded into 	*/
/*	centroid.  Modifies storage pointed to by cdbk, cdbkup, and 	*/
/*	trvect.								*/
/*  CALLS								*/
/*  	distor(), cenupdt(), cenrplc()					*/
/*									*/
/************************************************************************/

#include "stdvq.h"

double initialize (cdbk, cdbkup, trvect, trvectin)

    double* cdbk;	/* codebook					*/
    double* cdbkup;	/* upadate storage for codebook			*/
    double* trvect;	/* training sequence vector			*/
    DATATYPE* trvectin;	/* training sequence vector (input)		*/

{

    long t;		/* iteration counter				*/
    double dist;	/* distortion					*/
    int d;		/* iteration counter				*/
    int int_temp;	/* temporary storage				*/

    for (d = 0; d < cdbkdim; d++) cdbk[d] = 0.0;
    for (d = 0; d < cdbkupdim; d++) cdbkup[d] = 0.0;
    
    rewind(trsqfile);
    for (t = 0;; t++) {
        if (fread(trvectin, sizeof(DATATYPE), dim, trsqfile) != dim) break;
        for (d = 0; d < dim; d++) trvect[d] = (double) trvectin[d];
        cenupdt(cdbkup, trvect);
    }
    cenrplc(cdbkup, cdbk, t);
    rewind(trsqfile);
    for (t = 0;; t++) {
        if (fread(trvectin, sizeof(DATATYPE), dim, trsqfile) != dim) break;
        for (d = 0; d < dim; d++) trvect[d] = (double) trvectin[d];
	dist += fullsearch(cdbk, 1, trvect, &int_temp);
    }
    dist /= (double) t;
    return(dist);
}       

